var express = require('express')
  , routes = require('./routes')
  , nowjs = require('now')
  , eyes  = require('eyes');

// var SysLogger = require('ain2');
// var console = new SysLogger();

// console.log('notice: %d', Date.now());
// console.info('info');
// console.error('error');

var app = module.exports = express.createServer();
sessions = [];

// Configuration
app.configure(function(){
  app.set('views', __dirname + '/views');
  app.set('view engine', 'jade');
  app.use(express.bodyParser());
  app.use(express.cookieParser());
  app.use(express.session({ secret: "banana cake" }));
  app.use(express.methodOverride());
  app.use(require('stylus').middleware({ src: __dirname + '/public' }));
  app.use(app.router);
  app.use(express.static(__dirname + '/public'));
});

app.configure('development', function(){
  app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
});

app.configure('production', function(){
  app.use(express.errorHandler());
});

app.dynamicHelpers({session: function (req, res) {return req.session;}});

app.get('/logout', routes.logout);
app.post('/login/do', routes.loginDo);
app.get('/login/do', routes.login);
app.get('/login', routes.login);
app.get('/', routes.index);
app.get('/newIdea', routes.newIdea);
app.post('/saveCookie',routes.saveCookie);
app.get('/fail', function(req,res) {someUnexistingMethodCall();});

app.listen(3000);

// Now.js & Dual.js
var everyone = nowjs.initialize(app);
// require("./dual.js")(everyone, sessions[0].user);
var db = require("./database.js");

everyone.now.createIdea = function(opts) {
    db.projects.insert({
        title: opts.title, 
        description: opts.description,
        phase: 'idea'
    });

    this.now.notify({
        title: "Idea Created",
        text: "<h4>"+opts.title+"</h4><p>"+opts.description+"</p>"
    });
};
everyone.now.getProjects = function() {
  
}
console.log("Express server listening on port %d in %s mode", app.address().port, app.settings.env);